﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HRM
{
    public partial class frmInsuranceInfo : Form
    {
        string Detail_InsBookNo;
        string FullName;
        string Detail_Rate;
        string Detail_NewBasicFactor;
        string Detail_NewPositionFactor;
        string Detail_NewOverFactor;
        string Detail_NewJobFactor;
        string Detail_NewAreaFactor;
        string Detail_NewAmount;
        DataTable dtQTBH = null;

        public delegate bool RequireSaveDetail(DataTable _dtQTBH);
        public delegate void AfterEnterOnDatagridRow(DateTime fromDate, DateTime toDate);

        public event RequireSaveDetail requireSaveDetail;
        public event AfterEnterOnDatagridRow afterEnterOnDatagridRow;

        public frmInsuranceInfo(DataTable _dtQTBH, 
                string _Detail_InsBookNo, 
                string _FullName,
                string _Detail_Rate,
                string _Detail_NewBasicFactor,
                string _Detail_NewPositionFactor,
                string _Detail_NewOverFactor,
                string _Detail_NewJobFactor,
                string _Detail_NewAreaFactor,
                string _Detail_NewAmount
                )
        {
            InitializeComponent();
            //
            dgvList2.ReadOnly = false;
                dtQTBH = _dtQTBH;
             Detail_InsBookNo = _Detail_InsBookNo ;
             FullName = _FullName;
             Detail_Rate = _Detail_Rate;
             Detail_NewBasicFactor = _Detail_NewBasicFactor;
             Detail_NewPositionFactor =_Detail_NewPositionFactor;
             Detail_NewOverFactor = _Detail_NewOverFactor;
             Detail_NewJobFactor = _Detail_NewJobFactor;
             Detail_NewAreaFactor = _Detail_NewAreaFactor;
             Detail_NewAmount = _Detail_NewAmount;
            //

            lblDetail_InsBookNo.Text = Detail_InsBookNo;
            lblFullName.Text = FullName;

            BuildVoucherGrid();
            LoadList();
        }


        private void BuildVoucherGrid()
        {
           
            //UISetting.AddColumn(ref dgvList, "IsInsert", "Chọn", "C", 30, true, true);
            DataGridViewCheckBoxColumn cbxColumn = new DataGridViewCheckBoxColumn(true);
            cbxColumn.HeaderText = "Chọn";
            cbxColumn.Name = "IsInsert";
            cbxColumn.Visible = true;
            cbxColumn.ReadOnly = false;
            cbxColumn.ValueType = true.GetType();
            cbxColumn.ThreeState = false;
            cbxColumn.Width = 30;
            dgvList2.Columns.Add(cbxColumn);

            UISetting.AddColumn(ref dgvList2, "Detail_WorkUnit", "Đơn vị", "C", 80, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_FromDate", "Từ tháng", "C", 80, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_ToDate", "Đến tháng", "C", 90, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_TotalMonth", "Số tháng", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_Type", "HS/ML", "R", 40, true, true);
            
            UISetting.AddColumn(ref dgvList2, "Detail_NewAmount", "ML mới", "R", 80, false, true);
            UISetting.AddColumn(ref dgvList2, "Detail_OldAmount", "ML cũ", "R", 80, true, true);

            UISetting.AddColumn(ref dgvList2, "Detail_NewBasicFactor", "HSL mới", "R", 80, false, true);
            UISetting.AddColumn(ref dgvList2, "Detail_NewPositionFactor", "PC CV mới", "R", 80, false, true);
            UISetting.AddColumn(ref dgvList2, "Detail_NewOverFactor", "PC VK mới", "R", 80, false, true);
            UISetting.AddColumn(ref dgvList2, "Detail_NewJobFactor", "PC Nghề mới", "R", 80, false, true);
            UISetting.AddColumn(ref dgvList2, "Detail_NewAreaFactor", "PC KV mới", "R", 80, false, true);

            UISetting.AddColumn(ref dgvList2, "Detail_OldBasicFactor", "HSL cũ", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_OldPositionFactor", "PC CV cũ", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_OldOverFactor", "PC VK cũ", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_OldJobFactor", "PC Nghề cũ", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_OldAreaFactor", "PC KV cũ", "R", 80, true, true);

            UISetting.AddColumn(ref dgvList2, "Detail_NewTotal", "Tổng HS mới", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_OldTotal", "Tổng HS cũ", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_SocialRate", "Tỷ lệ", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_InsTypeCode", "PA", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList2, "Detail_Job", "Chức vụ", "R", 80, true, true);
        }

        void LoadList()
        {
            int n = dtQTBH.Rows.Count;
            dgvList2.Rows.Clear();
            for (int i = 0; i < n; i++)
            {
                dgvList2.Rows.Insert(i, 1);
                //Nếu PA = TS,OF thì ko check
                //Detail_Type khac _Type thi ko check
                //Detail_WorkUnit   is null  && Detail_WorkUnit # Branch_Code hien tai =>ko check
                string PA = Convert.ToString(dtQTBH.Rows[i]["Detail_InsTypeCode"]);
                string Detail_Type = Convert.ToString(dtQTBH.Rows[i]["loaips"]);
                string Detail_WorkUnit = Convert.ToString(dtQTBH.Rows[i]["Premium_WorkUnit"]);

                dgvList2.Rows[i].Cells["IsInsert"].ReadOnly = false;

                if (PA == "TS" || PA == "OF" || Detail_WorkUnit == "" || Detail_WorkUnit != Program.strBranchCode)
                {
                    dgvList2.Rows[i].Cells["IsInsert"].Selected = false;
                    dgvList2.Rows[i].Cells["IsInsert"].Value = false;
                    dgvList2.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                }
                else
                {
                    dgvList2.Rows[i].Cells["IsInsert"].Value = true;
                }


                dgvList2.Rows[i].Cells["Detail_WorkUnit"].Value = Convert.ToString(dtQTBH.Rows[i]["Premium_WorkUnit"]);

                dgvList2.Rows[i].Cells["Detail_FromDate"].Value = Convert.ToDateTime(dtQTBH.Rows[i]["Premium_FromDate"]).ToString("MM/yyyy");
                dgvList2.Rows[i].Cells["Detail_ToDate"].Value = Convert.ToDateTime(dtQTBH.Rows[i]["Premium_ToDate"]).ToString("MM/yyyy");
                dgvList2.Rows[i].Cells["Detail_TotalMonth"].Value = dtQTBH.Rows[i]["Premium_MonthNo"].ToString();
                dgvList2.Rows[i].Cells["Detail_Type"].Value = Convert.ToString(dtQTBH.Rows[i]["loaips"]);

                dgvList2.Rows[i].Cells["Detail_NewAmount"].Value = Detail_NewAmount;
                dgvList2.Rows[i].Cells["Detail_OldAmount"].Value = GetDecimalValueUtil(dtQTBH.Rows[i]["Premium_Salary"],"#,##0");

                if (PA == "TS" || PA == "OF" || Detail_WorkUnit == "" || Detail_WorkUnit != Program.strBranchCode)
                {
                    dgvList2.Rows[i].Cells["Detail_OldBasicFactor"].Value = 0;
                    dgvList2.Rows[i].Cells["Detail_OldPositionFactor"].Value = 0;
                    dgvList2.Rows[i].Cells["Detail_OldOverFactor"].Value = 0;
                    dgvList2.Rows[i].Cells["Detail_OldJobFactor"].Value = 0;
                    dgvList2.Rows[i].Cells["Detail_OldAreaFactor"].Value = 0;
                }
                else
                {
                    dgvList2.Rows[i].Cells["Detail_OldBasicFactor"].Value = GetDecimalValueUtil(dtQTBH.Rows[i]["Premium_BasicFactor"], UIParamater.Cham3So);
                    dgvList2.Rows[i].Cells["Detail_OldPositionFactor"].Value = GetDecimalValueUtil(dtQTBH.Rows[i]["Premium_PositionFactor"], UIParamater.Cham3So);
                    dgvList2.Rows[i].Cells["Detail_OldOverFactor"].Value = GetDecimalValueUtil(dtQTBH.Rows[i]["Premium_OverSeniorityFactor"], UIParamater.Cham3So);
                    dgvList2.Rows[i].Cells["Detail_OldJobFactor"].Value = GetDecimalValueUtil(dtQTBH.Rows[i]["Premium_SeniorityFactor"], UIParamater.Cham3So);
                    dgvList2.Rows[i].Cells["Detail_OldAreaFactor"].Value = GetDecimalValueUtil(dtQTBH.Rows[i]["Premium_AreaFactor"], UIParamater.Cham3So);
                }

                dgvList2.Rows[i].Cells["Detail_NewBasicFactor"].Value = Detail_NewBasicFactor;
                dgvList2.Rows[i].Cells["Detail_NewPositionFactor"].Value = Detail_NewPositionFactor;
                dgvList2.Rows[i].Cells["Detail_NewOverFactor"].Value = Detail_NewOverFactor;
                dgvList2.Rows[i].Cells["Detail_NewJobFactor"].Value = Detail_NewJobFactor;
                dgvList2.Rows[i].Cells["Detail_NewAreaFactor"].Value = Detail_NewAreaFactor;

                //dgvList.Rows[i].Cells["Detail_NewTotal"].Value = (Convert.ToDecimal(dtQTBH.Rows[i]["Detail_NewBasicFactor"]) + Convert.ToDecimal(dtQTBH.Rows[i]["Detail_NewPositionFactor"]) + Convert.ToDecimal(dtQTBH.Rows[i]["Detail_NewOverFactor"]) + Convert.ToDecimal(dtQTBH.Rows[i]["Detail_NewJobFactor"]) + Convert.ToDecimal(dtQTBH.Rows[i]["Detail_NewAreaFactor"])).ToString(UIParamater.Cham3So);
                //dgvList.Rows[i].Cells["Detail_OldTotal"].Value = (Convert.ToDecimal(dtQTBH.Rows[i]["Detail_OldBasicFactor"]) + Convert.ToDecimal(dtQTBH.Rows[i]["Detail_OldPositionFactor"]) + Convert.ToDecimal(dtQTBH.Rows[i]["Detail_OldOverFactor"]) + Convert.ToDecimal(dtQTBH.Rows[i]["Detail_OldJobFactor"]) + Convert.ToDecimal(dtQTBH.Rows[i]["Detail_OldAreaFactor"])).ToString(UIParamater.Cham3So);
                dgvList2.Rows[i].Cells["Detail_InsTypeCode"].Value = Convert.ToString(dtQTBH.Rows[i]["Detail_InsTypeCode"]);
                dgvList2.Rows[i].Cells["Detail_Job"].Value = Convert.ToString(dtQTBH.Rows[i]["Premium_Job"]);
                dgvList2.Rows[i].Cells["Detail_SocialRate"].Value = Detail_Rate;

                if (dtQTBH.Rows[i]["Premium_WorkUnit"] == DBNull.Value)
                    dgvList2.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
            }
        }

        protected string GetDecimalValueUtil(object obj,string Format)
        {
            if (obj == null || obj == DBNull.Value)
                return "";
            else
                return Convert.ToDecimal(obj).ToString(Format);

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn tạo phát sinh cho các giai đoạn này", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                DataTable _dtQTBH = new DataTable();
                _dtQTBH.Columns.Add("Detail_WorkUnit", typeof(string));
                _dtQTBH.Columns.Add("Detail_FromDate", typeof(string));
                _dtQTBH.Columns.Add("Detail_ToDate", typeof(string));
                _dtQTBH.Columns.Add("Detail_TotalMonth", typeof(int));
                _dtQTBH.Columns.Add("Detail_Type", typeof(string));
                _dtQTBH.Columns.Add("Detail_NewAmount", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_OldAmount", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_NewBasicFactor", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_NewPositionFactor", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_NewOverFactor", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_NewJobFactor", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_NewAreaFactor", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_OldBasicFactor", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_OldPositionFactor", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_OldOverFactor", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_OldJobFactor", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_OldAreaFactor", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_NewTotal", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_OldTotal", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_SocialRate", typeof(decimal));
                _dtQTBH.Columns.Add("Detail_Job", typeof(string));

                foreach (DataGridViewRow dgrv in dgvList2.Rows)
                {
                    if (Convert.ToBoolean(dgrv.Cells["IsInsert"].Value) == true)
                    {
                        _dtQTBH.Rows.Add(
                            dgrv.Cells["Detail_WorkUnit"].Value,
                            dgrv.Cells["Detail_FromDate"].Value,
                            dgrv.Cells["Detail_ToDate"].Value,
                            dgrv.Cells["Detail_TotalMonth"].Value,
                            dgrv.Cells["Detail_Type"].Value,
                            dgrv.Cells["Detail_NewAmount"].Value,
                            dgrv.Cells["Detail_OldAmount"].Value,
                            dgrv.Cells["Detail_NewBasicFactor"].Value,
                            dgrv.Cells["Detail_NewPositionFactor"].Value,
                            dgrv.Cells["Detail_NewOverFactor"].Value,
                            dgrv.Cells["Detail_NewJobFactor"].Value,
                            dgrv.Cells["Detail_NewAreaFactor"].Value,
                             dgrv.Cells["Detail_OldBasicFactor"].Value,
                            dgrv.Cells["Detail_OldPositionFactor"].Value,
                            dgrv.Cells["Detail_OldOverFactor"].Value,
                            dgrv.Cells["Detail_OldJobFactor"].Value,
                            dgrv.Cells["Detail_NewTotal"].Value,
                            dgrv.Cells["Detail_OldTotal"].Value,
                            dgrv.Cells["Detail_SocialRate"].Value,
                            dgrv.Cells["Detail_Job"].Value
                        );
                    }
                }
                
                if(requireSaveDetail != null)
                    requireSaveDetail(_dtQTBH);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void frmInsuranceInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Close();
                    break;
            }
        }

        private void dgvList2_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
             
             
        }

        private void dgvList2_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    DateTime fromDate = UISetting.GetDate(dgvList2.CurrentRow.Cells["Detail_FromDate"].Value.ToString());
                    DateTime toDate = UISetting.GetDate(dgvList2.CurrentRow.Cells["Detail_ToDate"].Value.ToString());
                    if (afterEnterOnDatagridRow != null)
                        afterEnterOnDatagridRow(fromDate, toDate);
                    this.Close();
                    break;

                case Keys.Escape:
                    this.Close();
                    break;
            }
        }
    }
}
